<?php
/**
 * Created by PhpStorm.
 * User: majiawen
 * Date: 2018/3/4
 * Time: 12:06
 */

namespace app\admin\controller\v1;

use app\admin\controller\Fun;
use app\common\Model\User;
use app\common\Model\UserShaohuo;
use app\common\Model\UserShaohuoFuncs;
use think\Db;

/**
 * @title 后台-捎货管理-捎货人管理-捎货人管理列表-V1
 * @description 接口说明
 *
 * @header name:copyright require:1 default:v1 desc:版本号(全局)
 */
class ShaohuoUser extends Base
{
    //有效时间（一月）
    const EXPIRE = 2678400;
    /**
     * @title 捎货人管理列表查询
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:ShaohuoUser desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:ShaohuoUserList desc:捎货人管理列表查询
     *
     * @param name:page type:string require:1 default: other: desc:页码
     * @param name:search type:string require:1 default: other: desc:查询数据
     * @param name:sort_field type:string require:1 default: other: desc:排序字段
     * @param name:sort_type type:string require:1 default: other: desc:0正序,1倒序
     * @param name:apply_status type:string require:1 default: other: desc:审核状态:0为审核中,1为通过,2为拒绝
     * @param name:exam_status type:string require:1 default: other: desc:考试状态:1为通过,0为未通过
     * @param name:status type:string require:1 default: other: desc:捎货人状态:1有效,0禁用
     * @param name:start_time type:string require:1 default: other: desc:用户注册开始查询时间
     * @param name:end_time type:string require:1 default: other: desc:用户注册结束查询时间
     *
     * @return data:[]
     */
    public function ShaohuoUserList()
    {
        //查询
        $where = [];
        if (isset($this->param['search']) && $this->param['search'] != '') {
            $search = $this->param['search'];
            $where['mobile|real_name|id_card|emergency_contact|emergency_contact_phone|remark'] = ['like', '%' . $search . '%'];
        }
        //审核状态:0为审核中,1为通过,2为拒绝
        if (isset($this->param['apply_status']) && $this->param['apply_status'] != '') {
            $where['s.apply_status'] = intval($this->param['apply_status']);
        }
        //考试状态:1为通过,0为未通过
        if (isset($this->param['exam_status']) && $this->param['exam_status'] != '') {
            //考试不通过状态
            if($this->param['exam_status'] == 0){
                $this->param['exam_status'] = [0,1,3,7];
            }
            $where['s.exam_status'] = ['in', $this->param['exam_status']];
        }
        //捎货人状态:1有效,0禁用
        if (isset($this->param['status']) && $this->param['status'] != '') {
            $where['s.status'] = intval($this->param['status']);
        }
        $where_time = '';
        //用户注册时间
        if (isset($this->param['start_time']) && $this->param['start_time'] != '') {
            $start_time = $this->param['start_time'];
            $where_time = "s.create_time >= '$start_time'";
        }
        if (isset($this->param['end_time']) && $this->param['end_time'] != '') {
            $end_time = $this->param['end_time'];
            if (strlen($where_time) > 5) {
                $where_time .= "AND s.create_time <= '$end_time'";
            } else {
                $where_time = "s.create_time <= '$end_time'";
            }
        }
        //可排序字段
        $used_sort_field = [
            'uid',
            'real_name',
            'mobile',
            'id_card',
            'status',
            'emergency_contact',
            'emergency_contact_phone',
            'idcard_front',
            'idcard_back',
            'vehicle_license',
            'driver_license',
            'apply_status',
            'exam_status',
            'remark',
            'create_time',
            'name',
        ];
        if (isset($this->param['sort_field']) && $this->param['sort_field'] != '' && in_array($this->param['sort_field'], $used_sort_field) && isset($this->param['sort_type'])) {
            if ($this->param['sort_field'] == 'uid') {
                $this->param['sort_field'] = 'u.uid';
            }
            $order = [
                $this->param['sort_field'] => $this->sort_type[intval($this->param['sort_type'])]
            ];
        } else {
            $order = ['u.uid' => $this->sort_type[1]];
        }
        $list = Db::name('user_shaohuo s')
            ->join([
                ['user u', 'u.uid=s.uid'],
            ])
            ->where($where)
            ->where($where_time)
            ->order($order)
            ->field('
            u.uid,
            s.realname as real_name,
            u.mobile,
            s.id_card,
            s.status,
            s.emergency_contact,
            s.emergency_contact_phone,
            s.idcard_front,
            s.idcard_back,
            s.vehicle_license,
            s.driver_license,
            s.apply_status,
            s.exam_status,
            s.remark,
            s.create_time
            ')
            ->paginate($this->page_size, false, ['page' => $this->param['page']])->toArray();
        if ($list) {
            $UserShaohuo = new UserShaohuo();
            //捎货人状态:1有效,0禁用
            $su_status = $UserShaohuo->_status;
            //审核状态:0为审核中,1为通过,2为拒绝
            $su_apply_status = $UserShaohuo->_apply_status;

            //考试状态:1为通过,0为未通过
            $su_exam_status = $UserShaohuo->_exam_status;
            $f_where = [
                'uf.status' => UserShaohuoFuncs::STATUS_NORMAL,
                'f.status' => \app\common\Model\FuncClass::STATUS_NORMAL,
                'f.is_del' => \app\common\Model\FuncClass::IS_DEL_FALSE,
            ];

            foreach ($list['data'] as $key => $value) {

                $list['data'][$key]['status'] = $su_status[$value['status']];
                $list['data'][$key]['apply_status'] = $su_apply_status[$value['apply_status']];
                $list['data'][$key]['exam_status'] = $su_exam_status[$value['exam_status']];
                $list['data'][$key]['funcs'] = [];
                //查询该捎货人所拥有的捎货业务
                $fucs = Db::name('user_shaohuo_funcs uf')
                    ->join('func_class fc','uf.fc_id=fc.fc_id')
                    ->where('uf.uid', $value['uid'])
                    ->where('uf.status', 1)
                    ->field('fc.fc_id,fc.func_name')
                    ->select();
                if(!empty($fucs)){
                    foreach ($fucs as $v){
                        array_push($list['data'][$key]['funcs'],$v);
                    }
                }

                if (empty($value['real_name'])) {
                    $detail = Db::name('user_shaohuo')->where(['uid'=>$value['uid']])->field('realname')->find();
                    $list['data'][$key]['real_name'] = $detail['realname'];
                }
            }
            return Fun::success(['list' => $list['data'], 'total_num' => $list['total']]);
        } else {
            return Fun::error('333333');//TODO 无数据，定义错误类型
        }
    }

    /**
     * @title 审核捎货人
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:ShaohuoUser desc:接口组(全局)
     * @header name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:command type:string require:1 default: other:applyShaohuoUser desc:审核捎货人
     *
     * @param name:ids type:string require:1 default: other:10086,10087,10088 desc:用户id
     * @param name:status type:string require:1 default: other:0 desc:审核状态:0为审核中,1为通过,2为拒绝
     * @param name:remark type:string require:1 default: other:0 desc:审核备注(拒绝时传备注)
     *
     * @return data:[]
     */
    public function applyShaohuoUser()
    {
        $ids = explode(',', trim($this->param['ids'], ','));
        $updata = [];

        //用户状态:1为有效,0为禁用
        if (!empty($this->param['status'])) {
            $updata['apply_status'] = $this->param['status'];
            if ($this->param['status'] == 3) {
                $updata['remark'] = $this->param['remark'];
            } else {
                $updata['remark'] = '';
            }
        }
        if (count($updata) && count($ids)) {
            $UserShaohuo = New UserShaohuo();
            $res = $UserShaohuo->where('uid', 'in', $ids)
                ->update($updata);
            if ($res) {
                return Fun::success('');
            }
        }
        return Fun::error('333332');//TODO 无数据，定义错误类型
    }

    /**
     * @title 捎货人禁用恢复
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:ShaohuoUser desc:接口组(全局)
     * @header name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:command type:string require:1 default: other:alterShaohuoUserStatus desc:捎货人禁用恢复
     *
     * @param name:ids type:string require:1 default: other:10086,10087,10088 desc:用户id
     * @param name:status type:string require:1 default: other:0 desc:捎货人状态:1有效,2禁用
     *
     * @return data:[]
     */
    public function alterShaohuoUserStatus()
    {
        $ids = explode(',', trim($this->param['ids'], ','));
        $updata = [];

        $UserShaohuo = New UserShaohuo();
        //用户状态:1为有效,2为禁用
        if (isset($this->param['status']) && in_array(intval($this->param['status']), [$UserShaohuo::STATUS_NORMAL, $UserShaohuo::STATUS_FORBID])) {
            $updata['status'] = $this->param['status'];
        }
        if (count($updata) && count($ids)) {
            $res = $UserShaohuo->where('uid', 'in', $ids)
                ->update($updata);
            if ($res) {
                return Fun::success('');
            }
        }
        return Fun::error('333332');//TODO 无数据，定义错误类型
    }

    /**
     * @title 添加修改捎货人
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:ShaohuoUser desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:addShaohuoUser desc:添加捎货人
     *
     * @param name:mobile type:string require:1 default: other: desc:手机号
     * @param name:real_name type:string require:1 default: other: desc:真实姓名
     * @param name:id_card type:string require:1 default: other: desc:身份证号
     * @param name:emergency_contact type:string require:1 default: other: desc:紧急联系人(图片连接)
     * @param name:emergency_contact_phone type:string require:1 default: other: desc:紧急联系人电话(图片连接)
     * @param name:idcard_front type:string require:1 default: other: desc:身份证正面(图片连接)
     * @param name:idcard_back type:string require:1 default: other: desc:身份证正面(图片连接)
     * @param name:vehicle_license type:string require:1 default: other: desc:行驶证(图片连接)
     * @param name:driver_license type:string require:1 default: other: desc:行驶证(图片连接)
     * @param name:fc_ids type:string require:1 default: other:1,2,3 desc:捎货业务ID
     *
     * @return data:[]
     */
    public function addShaohuoUser()
    {
        $User = new User();
        $UserShaohuo = new UserShaohuo();
        $UserShaohuoFuncs = new UserShaohuoFuncs();

        $res = $User->where('mobile', $this->param['mobile'])->find();

        $fc_ids = explode(',', trim($this->param['fc_ids'], ','));

        if (!$res) {
            $new = $fc_ids;
            //去判断用户是否存在用户
            $res = $userfind = $User->where('mobile', $this->param['mobile'])->find();
            if ($userfind) {
                $User->save([
                    'real_name'  => $this->param['real_name'],
                ],['uid' => $userfind['uid']]); 
            }else{
                $User->mobile = $this->param['mobile'];
                $User->real_name = $this->param['real_name'];
                $User->create_time = date('Y-m-d H:i:s',time());
                $User->update_time = date('Y-m-d H:i:s',time());
                $User->save();
                $newUserid = $User->uid;
                $str = time() . $newUserid . rand(1000,9999);
                $token = md5($str);
                $token_expired = date('Y-m-d H:i:s',time() + self::EXPIRE);
                $User->save([
                    'access_token'  => $token,
                    'token_expired' => $token_expired
                ],['uid' => $newUserid]);    
                $res = $User->where('uid', $newUserid)->find();
            }
        }else{
            $U_res = $UserShaohuo->where('uid', $res->uid)->find();

            //修改
            if ($U_res) {
                $UserShaohuo = $U_res;

                //捎货人所有的接单类型
//                $fc_id_ed = Db::table('sh_user_shaohuo_funcs')->where(['uid' => $U_res->uid,])->select();
                $fc_id_ed = UserShaohuoFuncs::get(['uid' => $U_res->uid])->all();
               // pre($fc_id_ed);
                foreach ($fc_id_ed as $k => $v) {
                    if (in_array($v->fc_id, $fc_ids) && $v->status == $UserShaohuoFuncs::STATUS_FORBID) {
                        //禁用
                        $v->status = $UserShaohuoFuncs::STATUS_NORMAL;
                        $v->save();
                    } elseif (!in_array($v->fc_id, $fc_ids) && $v->status == $UserShaohuoFuncs::STATUS_NORMAL) {
                        //解禁
                        $v->status = $UserShaohuoFuncs::STATUS_FORBID;
                        $v->save();
                    }
                    $id[] = $v->fc_id;
                }
                $new = array_diff($fc_ids, $id);
//                pre($new);
                //修改用户信息
                $User->save([
                    'real_name'  => $this->param['real_name']
                ],['uid' => $res->uid]);      
            }else{
                $new = $fc_ids;
                //去判断用户是否存在用户
                $res = $userfind = $User->where('mobile', $this->param['mobile'])->find();
                if ($userfind) {
                    $User->save([
                        'real_name'  => $this->param['real_name'],
                    ],['uid' => $userfind['uid']]); 
                }else{
                    $User->mobile = $this->param['mobile'];
                    $User->real_name = $this->param['real_name'];
                    $User->create_time = date('Y-m-d H:i:s',time());
                    $User->update_time = date('Y-m-d H:i:s',time());
                    $User->save();
                    $newUserid = $User->uid;
                    $str = time() . $newUserid . rand(1000,9999);
                    $token = md5($str);
                    $token_expired = date('Y-m-d H:i:s',time() + self::EXPIRE);
                    $User->save([
                        'access_token'  => $token,
                        'token_expired' => $token_expired
                    ],['uid' => $newUserid]);     
                    $res = $User->where('uid', $newUserid)->find();   
                } 
            }   
        }


        // 启动事务
        Db::startTrans();
        try {


            //新增
            $list = [];
            foreach ($new as $k => $v) {
                $list[] = ['uid' => $res->uid, 'fc_id' => $v,'status' => 1];
            }
            $UserShaohuoFuncs->saveAll($list);
            $UserShaohuo->uid = $res->uid;
            $UserShaohuo->id_card = $this->param['id_card'];
            $UserShaohuo->emergency_contact = $this->param['emergency_contact'];
            $UserShaohuo->emergency_contact_phone = $this->param['emergency_contact_phone'];
            $UserShaohuo->idcard_front = $this->param['idcard_front'];
            $UserShaohuo->idcard_back = $this->param['idcard_back'];
            $UserShaohuo->vehicle_license = $this->param['vehicle_license'];
            $UserShaohuo->driver_license = $this->param['driver_license'];
            $UserShaohuo->save();
            // 提交事务
            Db::commit();
            return Fun::success('添加成功');
        } catch (\Exception $e) {
//             回滚事务
            Db::rollback();
            return Fun::error('333332');//TODO 无数据，定义错误类型
        }
    }

    /**
     * @title 修改捎货人
     * @description
     * @author jikesen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:ShaohuoUser desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:updateShaohuoUser desc:添加捎货人
     *
     * @param name:uid type:string require:1 default: other: desc:uid
     * @param name:real_name type:string require:1 default: other: desc:真实姓名
     * @param name:id_card type:string require:1 default: other: desc:身份证号
     * @param name:emergency_contact type:string require:1 default: other: desc:紧急联系人(图片连接)
     * @param name:emergency_contact_phone type:string require:1 default: other: desc:紧急联系人电话(图片连接)
     * @param name:idcard_front type:string require:1 default: other: desc:身份证正面(图片连接)
     * @param name:idcard_back type:string require:1 default: other: desc:身份证正面(图片连接)
     * @param name:vehicle_license type:string require:1 default: other: desc:行驶证(图片连接)
     * @param name:driver_license type:string require:1 default: other: desc:行驶证(图片连接)
     * @param name:fc_ids type:string require:1 default: other:1,2,3 desc:捎货业务ID
     *
     * @return data:[]
     */
    public function updateShaohuoUser()
    {
        //pre($this->param);
        //查询捎货人信息
        $ship_user = Db::table('sh_user_shaohuo')->where('uid',$this->param['uid'])->find();
        if(empty($ship_user)){
            return Fun::error('999','捎货人信息不存在');//TODO 无数据，定义错误类型
        }

        $UserShaohuo = new UserShaohuo();
        $UserShaohuoFuncs = new UserShaohuoFuncs();

        $func = explode(',',$this->param['fc_ids']);


        Db::startTrans();
        try {
            //删除
            $UserShaohuoFuncs->where('uid',$this->param['uid'])->delete();

            //新增
            $list = [];
            foreach ($func as $k => $v) {
                $list[] = ['uid' => $this->param['uid'], 'fc_id' => $v,'status'=>$UserShaohuoFuncs::STATUS_NORMAL];
            }
            $UserShaohuoFuncs->saveAll($list);

            $suser = model('app\common\Model\UserShaohuo');
            $suser->where('uid',$this->param['uid']);
            $suser->realname = trim($this->param['real_name']);
            $suser->id_card = trim($this->param['id_card']);
            $suser->emergency_contact = $this->param['emergency_contact'];
            $suser->emergency_contact_phone = trim($this->param['emergency_contact_phone']);
            $suser->idcard_front = $this->param['idcard_front'];
            $suser->idcard_back = $this->param['idcard_back'];
            $suser->vehicle_license = $this->param['vehicle_license'];
            $suser->driver_license = $this->param['driver_license'];
            $suser->isUpdate(true)->save();
            Db::commit();
            return Fun::success('更新成功');
        } catch (\Exception $e) {
            Db::rollback();
            return Fun::error('333332',$e->getMessage());//TODO 无数据，定义错误类型
        }

    }

    /**
     * @title 捎货人数据导出
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:ShaohuoUser desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:exportShaohuoUser desc:捎货人数据导出
     *
     * @param name:search type:string require:1 default: other: desc:查询数据
     * @param name:sort_field type:string require:1 default: other: desc:排序字段
     * @param name:sort_type type:string require:1 default: other: desc:0正序,1倒序
     * @param name:apply_status type:string require:1 default: other: desc:审核状态:0为审核中,1为通过,2为拒绝
     * @param name:exam_status type:string require:1 default: other: desc:考试状态:1为通过,0为未通过
     * @param name:status type:string require:1 default: other: desc:捎货人状态:1有效,0禁用
     * @param name:start_time type:string require:1 default: other: desc:用户注册开始查询时间
     * @param name:end_time type:string require:1 default: other: desc:用户注册结束查询时间
     *
     * @return data:[]
     */
    public function exportShaohuoUser()
    {
        set_time_limit(0);
        ini_set('memory_limit', '2048M');
        //查询
        $where = [];
        if (isset($this->param['search']) && $this->param['search'] != '') {
            $search = $this->param['search'];
            $where['real_name|id_card|emergency_contact|emergency_contact_phone|remark'] = ['like', '%' . $search . '%'];
        }
        //审核状态:0为审核中,1为通过,2为拒绝
        if (isset($this->param['apply_status']) && $this->param['apply_status'] != '') {
            $where['s.apply_status'] = intval($this->param['apply_status']);
        }
        //考试状态:1为通过,0为未通过
        if (isset($this->param['exam_status']) && $this->param['exam_status'] != '') {
            $where['s.exam_status'] = ['in', $this->param['exam_status']];
        }
        //捎货人状态:1有效,0禁用
        if (isset($this->param['status']) && $this->param['status'] != '') {
            $where['s.status'] = intval($this->param['status']);
        }
        $where_time = '';
        //用户注册时间
        if (isset($this->param['start_time']) && $this->param['start_time'] != '') {
            $start_time = $this->param['start_time'];
            $where_time = "s.create_time >= '$start_time'";
        }
        if (isset($this->param['end_time']) && $this->param['end_time'] != '') {
            $end_time = $this->param['end_time'];
            if (strlen($where_time) > 5) {
                $where_time .= "AND s.create_time <= '$end_time'";
            } else {
                $where_time = "s.create_time <= '$end_time'";
            }
        }
        //可排序字段
        $used_sort_field = [
            'uid',
            'real_name',
            'mobile',
            'id_card',
            'status',
            'emergency_contact',
            'emergency_contact_phone',
            'idcard_front',
            'idcard_back',
            'vehicle_license',
            'driver_license',
            'apply_status',
            'exam_status',
            'remark',
            'create_time',
            'name',
        ];
        if (isset($this->param['sort_field']) && $this->param['sort_field'] != '' && in_array($this->param['sort_field'], $used_sort_field) && isset($this->param['sort_type'])) {
            if ($this->param['sort_field'] == 'uid') {
                $this->param['sort_field'] = 'u.uid';
            }
            $order = [
                $this->param['sort_field'] => $this->sort_type[intval($this->param['sort_type'])]
            ];
        } else {
            $order = ['u.uid' => $this->sort_type[1]];
        }
        $list = Db::name('user_shaohuo s')
            ->join([
                ['user u', 'u.uid=s.uid'],
            ])
            ->where($where)
            ->where($where_time)
            ->order($order)
            ->field('
            u.uid,
            u.real_name,
            u.mobile,
            s.id_card,
            s.status,
            s.emergency_contact,
            s.emergency_contact_phone,
            s.idcard_front,
            s.idcard_back,
            s.vehicle_license,
            s.driver_license,
            s.apply_status,
            s.exam_status,
            s.remark,
            s.create_time
            ')->select();


        if ($list) {
            $file_name = './csv/' . date('YmdHis').'.csv';
            $header = ['姓名', '身份证号', '捎货类型', '手机号', '紧急联系人', '紧急联系人电话','证件正面照片','证件反面照片', '审核状态', '考试状态', '异常原因', '申请时间', '状态'];

            $UserShaohuo = new UserShaohuo();
            //捎货人状态:1有效,0禁用
            $su_status = $UserShaohuo->_status;
            //审核状态:0为审核中,1为通过,2为拒绝
            $su_apply_status = $UserShaohuo->_apply_status;
            //考试状态:1为通过,0为未通过
            $su_exam_status = $UserShaohuo->_exam_status;
            $f_where = [
                'uf.status' => UserShaohuoFuncs::STATUS_NORMAL,
                'f.status' => \app\common\Model\FuncClass::STATUS_NORMAL,
                'f.is_del' => \app\common\Model\FuncClass::IS_DEL_FALSE,
            ];
            $data = [];
            foreach ($list as $key => $value) {
                $value['status'] = $su_status[$value['status']];
                $value['apply_status'] = $su_apply_status[$value['apply_status']];
                $value['exam_status'] = $su_exam_status[$value['exam_status']];
                //查询该捎货人所拥有的捎货业务
                $f_where['uf.uid'] = $value['uid'];
                $value['funcs'] = Db::name('user_shaohuo_funcs uf')
                    ->join([
                        ['func_class f', 'f.fc_id=uf.fc_id'],
                    ])
                    ->where($f_where)
                    ->field('f.fc_id,f.func_name')
                    ->select();
                if (empty($value['real_name'])) {
                    $detail = Db::name('user_shaohuo')->where(['uid'=>$value['uid']])->field('realname')->find();
                    $value['real_name'] = $list[$key]['real_name'] = $detail['realname'];
                }

                $new['real_name'] = $value['real_name'];
                $new['id_card'] = $value['id_card'];
                $fstr = '';
                foreach ($value['funcs'] as $key1 => $value1) {
                    if ($key1==0) {
                        $fstr = $value1['func_name'];
                    }else{
                        $fstr .= '|'.$value1['func_name'];
                    }
                }
                $new['funcs'] = $fstr;
                $new['mobile'] = $value['mobile'];
                $new['emergency_contact'] = $value['emergency_contact'];
                $new['emergency_contact_phone'] = $value['emergency_contact_phone'];
                $new['idcard_front'] = $value['idcard_front'];
                $new['idcard_back'] = $value['idcard_back'];
                $new['apply_status'] = $value['apply_status'];
                $new['exam_status'] = $value['exam_status'];
                $new['remark'] = $value['remark'];
                $new['create_time'] = $value['create_time'];
                $new['status'] = $value['status'];
                $data[] = $new;
            }

            Fun::exportCsv($file_name,$header,$data);
            return Fun::success(['file_url'=>config('param.url').trim($file_name,'.')]);
        } else {
            return Fun::error('802002');//所选导出数据为空
        }
    }
}
